package me.firstapp.spi.bbs.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpMethod;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import me.firstapp.common.exception.ServiceException;
import me.firstapp.common.exception.StatusHouse;
import me.firstapp.common.json.JsonPage;
import me.firstapp.common.json.PageObject;
import me.firstapp.common.json.SingleObject;
import me.firstapp.common.module.bbs.JsonBbsNotification;
import me.firstapp.common.module.bbs.JsonShopNotification;
import me.firstapp.common.module.bbs.JsonSysNotification;
import me.firstapp.common.utils.NotificationJson;
import me.firstapp.spi.base.impl.BaseServiceImpl;
import me.firstapp.spi.bbs.NotificationService;

@Service
public class NotificationServiceImpl extends BaseServiceImpl implements NotificationService {

	@Override
	public SingleObject<NotificationJson> findMemberUnReadNotificationCounts(Long memberId) {
		SingleObject<NotificationJson> result = new SingleObject<NotificationJson>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			JSONObject resultObject = api(parameters, "/notification/findMemberUnReadNotificationCounts.do",
					HttpMethod.GET, "获取用户未读通知数量");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject jsonObject = resultObject.getJSONObject("object");
				NotificationJson notificationJson = JSONObject.toJavaObject(jsonObject, NotificationJson.class);
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				result.setObject(notificationJson);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonBbsNotification> findMemberBbsNotificationPage(Integer pageNo, Integer pageSize,
			Long memberId, Integer isRead) {
		PageObject<JsonBbsNotification> result = new PageObject<JsonBbsNotification>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("memberId", memberId);
			parameters.put("isRead", isRead);
			JSONObject resultObject = api(parameters, "/notification/findMemberBbsNotificationPage.do", HttpMethod.GET,
					"分页查询用户社区通知");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject pageJsonObject = resultObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonBbsNotification> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonBbsNotification> notificationList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonBbsNotification notification = JSONObject.toJavaObject(_jsonObject,
										JsonBbsNotification.class);
								notificationList.add(notification);
							}
						}
					}
					jsonPage.setList(notificationList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;

	}

	@Override
	public PageObject<JsonSysNotification> findMemberSysNotificationPage(Integer pageNo, Integer pageSize,
			Long memberId, Integer isRead) {
		PageObject<JsonSysNotification> result = new PageObject<JsonSysNotification>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("memberId", memberId);
			parameters.put("isRead", isRead);

			JSONObject resultObject = api(parameters, "/notification/findMemberSysNotificationPage.do", HttpMethod.GET,
					"分页查询用户系统通知");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject pageJsonObject = resultObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonSysNotification> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonSysNotification> notificationList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonSysNotification notification = JSONObject.toJavaObject(_jsonObject,
										JsonSysNotification.class);
								notificationList.add(notification);
							}
						}
					}
					jsonPage.setList(notificationList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public SingleObject<Object> setNotificationReaded(Long memberId, String type) {
		SingleObject<Object> result = new SingleObject<Object>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("memberId", memberId);
			parameters.put("type", type);
			JSONObject resultObject = api(parameters, "/notification/setNotificationReaded.do", HttpMethod.POST,
					"通知设为已读");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

	@Override
	public PageObject<JsonShopNotification> findMemberShopNotificationPage(Integer pageNo, Integer pageSize,
			Long memberId, Integer isRead) {
		PageObject<JsonShopNotification> result = new PageObject<JsonShopNotification>();
		try {
			Map<String, Object> parameters = new HashMap<>();
			parameters.put("pageNo", pageNo);
			parameters.put("pageSize", pageSize);
			parameters.put("memberId", memberId);
			parameters.put("isRead", isRead);

			JSONObject resultObject = api(parameters, "/notification/findMemberShopNotificationPage.do", HttpMethod.GET,
					"分页查询用户店铺通知");

			if (StatusHouse.COMMON_STATUS_OK.getCode().equals(resultObject.getString("code"))) {
				JSONObject pageJsonObject = resultObject.getJSONObject("page");
				if (pageJsonObject != null) {
					JsonPage<JsonShopNotification> jsonPage = new JsonPage<>();
					jsonPage.setPageNo(pageJsonObject.getIntValue("pageNo"));
					jsonPage.setPageSize(pageJsonObject.getIntValue("pageSize"));
					jsonPage.setTotalCount(pageJsonObject.getIntValue("totalCount"));
					List<JsonShopNotification> notificationList = new ArrayList<>();
					JSONArray jsonArray = pageJsonObject.getJSONArray("list");
					if (jsonArray != null) {
						for (int i = 0; i < jsonArray.size(); i++) {
							JSONObject _jsonObject = jsonArray.getJSONObject(i);
							if (_jsonObject != null) {
								JsonShopNotification notification = JSONObject.toJavaObject(_jsonObject,
										JsonShopNotification.class);
								notificationList.add(notification);
							}
						}
					}
					jsonPage.setList(notificationList);
					result.setPage(jsonPage);
					result.setStatusObject(StatusHouse.COMMON_STATUS_OK);
				}
			} else {
				throw new ServiceException(resultObject.getString("code"), resultObject.getString("msg"));
			}
		} catch (ServiceException e) {
			result.setStatusObject(e.getStatusObject());
		} catch (Exception e) {
			e.printStackTrace();
			result.setStatusObject(StatusHouse.COMMON_STATUS_ERROR);
		}
		return result;
	}

}
